package cn.iocoder.yudao.module.digital.dal.mysql.cardkeyproduct;

import java.util.*;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.digital.dal.dataobject.cardkeyproduct.CardkeyProductDO;
import cn.iocoder.yudao.module.digital.dal.dataobject.cardkeyproduct.CardkeyProductJoinDO;
import cn.iocoder.yudao.module.digital.dal.dataobject.cardkeysecret.CardkeySecretDO;
import cn.iocoder.yudao.module.digital.enums.cardkeysecret.CardKeySourceEnum;
import cn.iocoder.yudao.module.digital.enums.cardkeysecret.ClaimStatusEnum;
import cn.iocoder.yudao.module.digital.service.cardchannel.model.ProductResponse;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.digital.controller.admin.cardkeyproduct.vo.*;

/**
 * 卡密商品 Mapper
 *
 * @author 超管
 */
@Mapper
public interface CardkeyProductMapper extends BaseMapperX<CardkeyProductDO> {

    default List<CardkeyProductDO> selectBySource(CardKeySourceEnum sourceEnum) {
        return selectList(CardkeyProductDO::getSource, sourceEnum.getValue());
    }

    default PageResult<CardkeyProductDO> selectPage(CardkeyProductPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<CardkeyProductDO>()
                .eqIfPresent(CardkeyProductDO::getSource, reqVO.getSource())
                .eqIfPresent(CardkeyProductDO::getProductNo, reqVO.getProductNo())
                .likeIfPresent(CardkeyProductDO::getProductName, reqVO.getProductName())
                .orderByDesc(CardkeyProductDO::getId));
    }

    //
    default PageResult<CardkeyProductJoinDO> selectPageWithStock(CardkeyProductPageReqVO reqVO) {
        final MPJLambdaWrapper<CardkeyProductDO> wrapper = new MPJLambdaWrapper<CardkeyProductDO>()
                .selectAll(CardkeyProductDO.class)
                .selectCount(CardkeySecretDO::getId, CardkeyProductJoinDO::getStock)
                .eq(reqVO.getSource() != null, CardkeyProductDO::getSource, reqVO.getSource())
                .eq(StrUtil.isNotEmpty(reqVO.getProductNo()), CardkeyProductDO::getProductNo, reqVO.getProductNo())
                .like(StrUtil.isNotEmpty(reqVO.getProductName()), CardkeyProductDO::getProductName, reqVO.getProductName())
                .leftJoin(CardkeySecretDO.class, on -> on
                        .eq(CardkeySecretDO::getProductNo, CardkeyProductDO::getProductNo)
                        .eq(CardkeySecretDO::getClaimStatus, ClaimStatusEnum.NO.getValue()))
                .groupBy(CardkeyProductDO::getProductNo)
                .orderByDesc(CardkeyProductDO::getId);

        final PageResult<CardkeyProductJoinDO> result = selectJoinPage(reqVO, CardkeyProductJoinDO.class, wrapper);

        for (CardkeyProductJoinDO productJoinDO : result.getList()) {
            if (CardKeySourceEnum.fromValue(productJoinDO.getSource()) == CardKeySourceEnum.BLACK_HAWK_API) {
                if (productJoinDO.getStock() == 0) {
                    productJoinDO.setStock(null);
                }
            }

            String redemptionTexts = null;
            if (StrUtil.isNotEmpty(productJoinDO.getProductDetailData())) {
                final CardKeySourceEnum cardKeySourceEnum = CardKeySourceEnum.fromValue(productJoinDO.getSource());
                if (cardKeySourceEnum == CardKeySourceEnum.BLACK_HAWK_API) {
                    final ProductResponse detail = JsonUtils.parseObject(productJoinDO.getProductDetailData(), ProductResponse.class);
                    final ProductResponse.RedemptionCharacteristics redemptionCharacteristics = detail.getDetails().getRedemptionCharacteristics();
                    if (redemptionCharacteristics != null && CollUtil.isNotEmpty(redemptionCharacteristics.getRedemptionTexts())) {
                        redemptionTexts = redemptionCharacteristics.getRedemptionTexts().stream().map(v -> v.getText()).collect(Collectors.joining("\n"));
                    }
                }
            }

            productJoinDO.setRedemptionTexts(redemptionTexts);
        }
        return result;
    }

    default List<CardkeyProductDO> getCardKeyProductByProductNo(Collection<String> productNoSet) {
        final List<CardkeyProductDO> list = selectList(new LambdaQueryWrapperX<CardkeyProductDO>().in(CardkeyProductDO::getProductNo, productNoSet));
        return list;
    }

    default CardkeyProductDO getCardKeyProductByProductNo(String productNo) {
        final CardkeyProductDO productDO = selectOne(new LambdaQueryWrapperX<CardkeyProductDO>().in(CardkeyProductDO::getProductNo, productNo));
        return productDO;
    }

}